// pages/knowledge/knowledge.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 城市数据
    cityList: ['北京协和医院', '第一附属医院', '省医院', '市医院'],
    // 科室数据
    departmentList: ['内科', '外科', '儿科', '传染病科','妇产科','男科','精神科','皮肤病科','中医科'],
    // 排序方式数据
    sortList: ['综合排序', '评分', '咨询量', '价格'],
    // 当前选中的城市
    selectedCity: '北京协和医院',
    // 当前选中的科室
    selectedDepartment: '儿科',
    // 当前选中的排序方式
    selectedSort: '综合排序',
    // 控制城市下拉框的显示与隐藏
    showCityPicker: false,
    // 控制科室下拉框的显示与隐藏
    showDepartmentPicker: false,
    // 控制排序方式下拉框的显示与隐藏
    showSortPicker: false,
    cityIndex: 0,
    departmentIndex: 0,
    sortIndex: 0,
    allDoctors: [
      {
        id: 1,
        avatar: '../../../images/头1.jpg',
        name: '张清北',
        title: '知名专家',
        hospital: '北京协和医院',
        department: '儿科',
        specialty: '感冒、小儿支气管哮喘、小儿感冒、鼻炎、睡眠障碍',
        rating: '99.2',
        consultations: '236966',
        price: '49',
        isAvailable: true
      },
      {
        id: 2,
        avatar: '../../../images/头2.jpg',
        name: '韩梅梅',
        title: '知名专家',
        hospital: '北京协和医院',
        department: '儿科',
        specialty: '感冒、小儿支气管哮喘、小儿感冒、鼻炎、睡眠障碍',
        rating: '99.2',
        consultations: '236966',
        price: '49',
        isAvailable: false
      },
      {
        id: 3,
        avatar: '../../../images/头3.jpg',
        name: '张大三',
        title: '知名专家',
        hospital: '北京协和医院',
        department: '儿科',
        specialty: '感冒、小儿支气管哮喘、小儿感冒、鼻炎、睡眠障碍',
        rating: '99.2',
        consultations: '236966',
        price: '49',
        isAvailable: true
      },
      {
        id: 4,
        avatar: '../../../images/头4.jpg',
        name: '李思思',
        title: '主任医师',
        hospital: '北京协和医院',
        department: '儿科',
        specialty: '小儿消化系统疾病、营养不良、消化不良',
        rating: '98.7',
        consultations: '189654',
        price: '59',
        isAvailable: true
      },
      {
        id: 5,
        avatar: '../../../images/头5.jpg',
        name: '王医生',
        title: '副主任医师',
        hospital: '北京协和医院',
        department: '儿科',
        specialty: '小儿神经系统疾病、癫痫、发育迟缓',
        rating: '97.9',
        consultations: '156321',
        price: '45',
        isAvailable: true
      },
      {
        id: 6,
        avatar: '../../../images/头3.jpg',
        name: '赵医生',
        title: '主治医师',
        hospital: '第一附属医院',
        department: '儿科',
        specialty: '新生儿疾病、早产儿护理',
        rating: '96.8',
        consultations: '123654',
        price: '39',
        isAvailable: true
      },
      {
        id: 7,
        avatar: '../../../images/头3.jpg',
        name: '刘医生',
        title: '知名专家',
        hospital: '省医院',
        department: '儿科',
        specialty: '儿童过敏、哮喘、呼吸系统疾病',
        rating: '99.0',
        consultations: '210369',
        price: '55',
        isAvailable: false
      },
      {
        id: 8,
        avatar: '../../../images/头3.jpg',
        name: '陈医生',
        title: '主任医师',
        hospital: '市医院',
        department: '儿科',
        specialty: '儿童心脏病、先天性心脏病筛查',
        rating: '98.5',
        consultations: '178963',
        price: '50',
        isAvailable: true
      }
    ],
    doctors: [],
    isLoading: false,
    pageNum: 1 , // 添加页码状态
    pageSize: 5,  // 每页显示数量
    hasMoreData: true // 是否还有更多数据
  },

  toDoctor(){
    wx.navigateTo({
      url: '/findDoctor/pages/doctorDetail/doctorDetail',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.loadDoctors();
  },
  loadDoctors() {
    // 如果正在加载或没有更多数据，则返回
    if (this.data.isLoading || !this.data.hasMoreData) {
      return;
    }
    
    this.setData({
      isLoading: true
    });
    
    // 模拟网络请求延迟
    setTimeout(() => {
      const { allDoctors, pageNum, pageSize } = this.data;
      // 计算起始索引
      const startIndex = (pageNum - 1) * pageSize;
      // 计算结束索引
      const endIndex = startIndex + pageSize;
      // 获取当前页数据
      const currentPageData = allDoctors.slice(startIndex, endIndex);
      // 合并数据
      const newDoctors = pageNum === 1 ? currentPageData : this.data.doctors.concat(currentPageData);
      // 判断是否还有更多数据
      const hasMoreData = endIndex < allDoctors.length;
      
      this.setData({
        doctors: newDoctors,
        isLoading: false,
        hasMoreData: hasMoreData
      });
    }, 800); // 模拟网络请求时间
  },

  // 切换城市下拉框显示状态
  toggleCityPicker() {
    this.setData({
      showCityPicker: !this.data.showCityPicker,
      showDepartmentPicker: false,
      showSortPicker: false
    });
  },
  // 切换科室下拉框显示状态
  toggleDepartmentPicker() {
    this.setData({
      showDepartmentPicker: !this.data.showDepartmentPicker,
      showCityPicker: false,
      showSortPicker: false
    });
  },
  // 切换排序方式下拉框显示状态
  toggleSortPicker() {
    this.setData({
      showSortPicker: !this.data.showSortPicker,
      showCityPicker: false,
      showDepartmentPicker: false
    });
  },
  // 城市选择变化时触发
  onCityChange(e) {
    const index = e.detail.value[0];
    this.setData({
      selectedCity: this.data.cityList[index],
      cityIndex: index,
      showCityPicker: false,
      // 重置分页，重新加载数据
      pageNum: 1,
      doctors: []
     }, () => {
      this.loadDoctors();
    });
  },
  // 科室选择变化时触发
  onDepartmentChange(e) {
    const index = e.detail.value[0];
    this.setData({
      selectedDepartment: this.data.departmentList[index],
      departmentIndex: index,
      showDepartmentPicker: false,
      pageNum: 1,
      doctors: []
    }, () => {
      this.loadDoctors();
    });
  },
  // 排序方式选择变化时触发
  onSortChange(e) {
    const index = e.detail.value[0];
    this.setData({
      selectedSort: this.data.sortList[index],
      sortIndex: index,
      showSortPicker: false,
      pageNum: 1,
      doctors: []
    }, () => {
      this.loadDoctors();
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    // 下拉刷新，重置数据
    this.setData({
      pageNum: 1,
      doctors: [],
      hasMoreData: true
    }, () => {
      this.loadDoctors(() => {
        // 停止下拉刷新动画
        wx.stopPullDownRefresh();
      });
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    // 上拉触底时加载下一页数据
    if (this.data.hasMoreData) {
      this.setData({
        pageNum: this.data.pageNum + 1
      }, () => {
        this.loadDoctors();
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})